package com.bank.chat.common.aop;


import com.alibaba.fastjson.JSONObject;
import com.bank.chat.factory.EntityFactory;
import com.bank.chat.factory.MapperFactory;
import com.bank.chat.service.ChatRecordService;
import com.bank.model.entity.ChatRecord;
import com.bank.model.enums.ChatRecordType;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 聊天记录切面类
 *
 * @author yanpanyi
 * @date 2019/4/23
 */
@Aspect
@Component
//@Slf4j
public class ChatRecordAspect {
    @Autowired
    MapperFactory mapperFactory;
    @Autowired
    EntityFactory entityFactory;

    @Pointcut("@annotation(com.bank.chat.common.annotation.ChatRecordAnnotation)")
    public void chatRecordPointcut() {
    }

    @Before("chatRecordPointcut()")
    public void doBefore(JoinPoint joinPoint) {
        System.out.println("aop打开");
        JSONObject param = JSONObject.parseObject(joinPoint.getArgs()[0].toString());

        if(param.get("type").equals("REGISTER")){
            String fromUserId = param.get("userId").toString();
            System.out.println("用户：" + fromUserId +  "登录");
        }
        else if(param.get("type").equals("SINGLE_SENDING")){
            long fromId = Long.valueOf(param.get("fromId").toString());
            long toId = Long.valueOf(param.get("toId").toString()).longValue();
            int type = ChatRecordType.word.getCode();
            String content = param.get("content").toString();
            ChatRecord chatRecord = entityFactory.getchatRecord(fromId,toId,type,content);
            mapperFactory.getChatRecordMapper().insert(chatRecord);
        }
        else {
            System.out.println("others");
        }

//        log.debug("before -> {}", joinPoint);

//        MessageVO messageVO = null;
//        Object[] args = joinPoint.getArgs();
//        for (Object obj : args) {
//            if (obj instanceof MessageVO) {
//                messageVO = (MessageVO) obj;
//                break;
//            }
//        }
//
//        Assert.notNull(messageVO, "方法必需以MessageVO类或该类的子类作为参数");
//
//        if (messageVO.getType() == MessageTypeEnum.USER) {
//            // 对于User类型的消息做敏感词处理
//            messageVO.setMessage(SensitiveWordUtils.loveChina(messageVO.getMessage()));
//        }
//
//        log.debug("添加聊天记录 -> {}", messageVO);
//        chatRecordService.addRecord(ChatRecordDto.toChatRecordDto(messageVO));
    }

}
